<?php
namespace AdminBundle\Repository;


class BaseWebchatMemberRepository extends BaseRepository
{
    public function pageQuery($_params, $page)
    {
        $sql = <<<EOF
SELECT 
    m.id,
    m.webchat_user_id AS webchatUserId,
    m.nickname,
    m.user_name AS userName,
    m.remark_name AS remarkName,
    m.mobile,
    m.password,
    m.note,
    m.is_enabled AS isEnabled,
    m.creator,
    m.ip,
    m.ctime,
    m.country,
    m.province,
    m.city,
    m.signature,
    m.sex,
    m.head_img_url AS headImgUrl,
    m.status,
    m.status_text AS statusText,
    m.is_contact AS isContact,
    m.type,
    m.type_text AS typeText,
    u.nickname as ownerNickname
FROM
    base_webchat_member AS m
        right JOIN
    base_webchat_user AS u ON m.webchat_user_id = u.id
WHERE
    1 = 1
EOF;
        $params = array();
        if (!empty($_params)) {
            /*            if (isset($_params['starttime'])) {
                            $sql .= ' and m.ctime > :starttime ';
                            $params['starttime'] = $_params['starttime'];
                        }
                        if (isset($_params['endtime'])) {
                            $sql .= ' and m.ctime < :endtime ';
                            $params['endtime'] = $_params['endtime'];
                        }*/
            if (isset($_params['keyword']) && !empty($_params['keyword'])) {
                $sql .= ' and m.nickname like :keyword ';
                $params['keyword'] = '%' . $_params['keyword'] . '%';
            }
            if (isset($_params['wechat_user_id']) && !empty($_params['wechat_user_id'])) {
                $sql .= ' and m.webchat_user_id = :wechat_user_id';
                $params['wechat_user_id'] = $_params['wechat_user_id'];
            }

        }
        $sql .= ' ORDER BY id DESC';

        return $this->pageWithRawSql($sql, $params, $page);
    }

    /**
     * 获取进行日常养号聊天功能的熟人好友
     * @param $user_id
     * @param int $source_type
     * @param string $tags
     * @param int $count
     * @return bool
     * @throws \Exception
     */
    public function contactOfChat($user_id, $source_type = 1, $tags = '', $count = 10)
    {
        $sql = <<<EOF
SELECT 
    id, nickname, user_name AS userName, mobile
FROM
    base_webchat_member
WHERE
    is_enabled = 1 AND webchat_user_id = :webchat_user_id
    AND source_type = :source_type
ORDER BY RAND()    
LIMIT {$count}
EOF;
        $params = [
            'webchat_user_id' => $user_id,
            'source_type' => $source_type
        ];
        if (!empty($tags)) {
            $sql .= " AND m.tags like :tags ";
            $params['tags'] = '%' . $tags . '%';
        }

        return $this->rawQuery($sql, $params);
    }

    /**
     * 获取与聊天功能相关的营销好友
     * @param $user_id
     * @param int $source_type
     * @param string $tags
     * @param int $count
     * @return bool
     * @throws \Exception
     */
    public function labourOfChat($user_id, $source_type = 0, $tags = '', $count = 10)
    {
        $sql = <<<EOF
SELECT 
    m.id,
    m.nickname,
    m.user_name AS userName,
    m.mobile,
    m.tags as note
FROM
    base_webchat_member AS m
        LEFT JOIN
    log_contact_chat AS l ON m.webchat_user_id = l.webchat_user_id
        AND m.id = l.member_user_id
WHERE
    m.is_enabled = 1
        AND m.webchat_user_id = :webchat_user_id
        AND m.source_type = :source_type
        AND l.id IS NULL
EOF;
        $params = [
            'webchat_user_id' => $user_id,
            'source_type' => $source_type
        ];
        if (!empty($tags)) {
            $sql .= " AND m.tags like :tags ";
            $params['tags'] = '%' . $tags . '%';
        }
        $sql .= "ORDER BY id desc LIMIT {$count} ";

        return $this->rawQuery($sql, $params);
    }

    /**
     * 获取需要入群的营销好友
     * @param $user_id
     * @param int $source_type
     * @param string $tags
     * @param int $count
     * @return bool
     * @throws \Exception
     */
    public function labourJoinRoom($user_id, $source_type = 0, $tags = '', $count = 10)
    {
        $sql = <<<EOF
SELECT 
    m.id, m.nickname, m.user_name AS userName, m.mobile
FROM
    base_webchat_member AS m
        LEFT JOIN
    log_room_joined AS l ON m.webchat_user_id = l.webchat_id
        AND m.id = l.member_user_id
WHERE
    m.is_enabled = 1
        AND m.webchat_user_id = :webchat_user_id
        AND m.source_type = :source_type
        AND l.id IS NULL
EOF;
        $params = [
            'webchat_user_id' => $user_id,
            'source_type' => $source_type
        ];
        if (!empty($tags)) {
            $sql .= " AND m.tags like :tags ";
            $params['tags'] = '%' . $tags . '%';
        }
        $sql .= " ORDER BY RAND() LIMIT {$count} ";
        return $this->rawQuery($sql, $params);
    }
}
